package soderqvist.paulsson.logmaster;

import helpers.DateConverter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnDateChangeListener {
	private static final int NEW_NOTE = Menu.FIRST + 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		CalendarView calendarView = (CalendarView) findViewById(R.id.calendar);
		calendarView.setOnDateChangeListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_note_list, menu);
		menu.add(Menu.NONE, NEW_NOTE, Menu.NONE, "New note");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == NEW_NOTE) {
			Intent i = new Intent(this, AddNote.class);
			startActivity(i);
			return true;
		} else {
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onSelectedDayChange(CalendarView view, int year, int month,
			int day) {
		MyNewThread thread = new MyNewThread();
		thread.execute(DateConverter.getDate(year, month, day));
	}

	protected void createNoteList(String notes) {
		Intent intent = new Intent(this, NoteList.class);
		intent.putExtra("notes", notes);
		startActivity(intent);
	}

	protected void makeErrorToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	private class MyNewThread extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... date) {
			try {
				Socket socket = new Socket("85.24.188.49", 41253);
				InputStreamReader inputStreamReader = null;
				BufferedReader input = null;
				PrintStream output = null;
				try {
					inputStreamReader = new InputStreamReader(
							socket.getInputStream());
					input = new BufferedReader(inputStreamReader);
					output = new PrintStream(socket.getOutputStream());
					output.println("log-master request");
					output.println(date[0]);
					output.println("NOHOMO");
					output.flush();
					String lineRead = "";
					boolean success = false;
					String notes = "";
					while ((lineRead = input.readLine()) != null) {
						notes += lineRead;
						if (lineRead.contains("EY OK")) {
							success = true;
							break;
						}
					}
					output.close();
					input.close();
					inputStreamReader.close();
					socket.close();
					if (success) {
						createNoteList(notes);
					}
				} catch (IOException e) {
					makeErrorToast("Could not send request to server, try again later.");
				}
			} catch (UnknownHostException e) {
				makeErrorToast("Could not establish connection to the server, contact your server administator.");
			} catch (IOException e) {
				makeErrorToast("Port in use, reprogram the app if this happens.. :P");
			}
			return null;
		}
	}
}